//
//  FKShareChooseImageView.m
//  WanTao
//
//  Created by Macbook Pro on 2019/3/21.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKShareChooseImageView.h"
#import "FKCustomBtn.h"
#import "FKShareImageItemCell.h"
static NSString *const ID = @"FKShareImageItemCell";

@interface FKShareChooseImageView ()<UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, weak) UICollectionView *collectionView;
@property (nonatomic, weak) FKCustomBtn *bottomBtn;
//@property (nonatomic, strong) NSArray *imageViews;

@end

@implementation FKShareChooseImageView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews
{
    UIView *container = [[UIView alloc] init];
    [self addSubview:container];
    container.backgroundColor = [UIColor whiteColor];
    [container fk_viewCornerRadius:5 borderWidth:0 borderColor:nil];
//    container.bounds = CGRectMake(0, 0, fk_adjustW(324), fk_adjustW(380));
    
    UILabel *titleLabel = [UILabel fk_labelWithFont:fk_adjustFont(13) textColor:fkColor333333 text:@"文案已经复制到剪切板"];
    [container addSubview:titleLabel];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    CGFloat itemW = fk_adjustW(75);
    CGFloat itemH = fk_adjustW(88);
    layout.minimumLineSpacing = fk_adjustW(5) - 1;
    layout.minimumInteritemSpacing = fk_adjustW(20) - 1;
    layout.itemSize = CGSizeMake(itemW, itemH);
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    [container addSubview:collectionView];
    self.collectionView = collectionView;
    collectionView.dataSource = self;
    collectionView.delegate = self;
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.allowsMultipleSelection = true;
    [collectionView registerClass:[FKShareImageItemCell class] forCellWithReuseIdentifier:ID];
    
    UIButton *closeBtn = [UIButton fk_btnWithTarget:self action:@selector(dismiss) img:[UIImage imageNamed:@"close_icon"]];
    [container addSubview:closeBtn];
    closeBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    
    UILabel *bottomLabel = [UILabel fk_labelWithFont:fk_adjustFont(10) textColor:fkColor666666 text:@"请选择分享的图片"];
    [container addSubview:bottomLabel];
    
    FKCustomBtn *bottomBtn = [FKCustomBtn fk_btnWithTarget:self action:@selector(bottomBtnClicked) font:fk_adjustFont(10) normalTitle:nil normalTitleColor:fkColor666666 selectedTitle:nil selectedTitleColor:nil normalImg:nil selectedImg:nil bgImg:nil selectedBgImg:nil];
    [self addSubview:bottomBtn];
    self.bottomBtn = bottomBtn;
    bottomBtn.space = 10;
    bottomBtn.imagePosition = FKImagePositionUp;
    bottomBtn.backgroundColor = [UIColor whiteColor];
    [bottomBtn fk_viewCornerRadius:fk_adjustW(10) borderWidth:0 borderColor:nil];
    
    [bottomBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.bottom.offset(fk_adjustW(10));
        make.height.offset(fk_adjustW(98));
    }];
    [container mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(fk_adjustW(324));
        make.height.offset(fk_adjustW(380));
        make.bottom.equalTo(bottomBtn.mas_top).offset(fk_adjustW(-104));
        make.centerX.offset(0);
    }];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(fk_adjustW(30));
        make.top.offset(fk_adjustW(18));
    }];
    [closeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(fk_adjustW(36));
        make.right.offset(-12);
        make.centerY.equalTo(titleLabel);
    }];
    [collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(fk_adjustW(54));
        make.left.offset(fk_adjustW(30));
        make.right.offset(fk_adjustW(-30));
        make.bottom.offset(0);
    }];
    [bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(titleLabel);
        make.bottom.offset(fk_adjustW(-20));
    }];
}

- (void)setType:(NSInteger)type
{
    _type = type;
    
    if (type == 1) {
        [self.bottomBtn setTitle:@"分享给微信朋友圈" forState:UIControlStateNormal];
        [self.bottomBtn setImage:[UIImage imageNamed:@"wechat_timeline"] forState:UIControlStateNormal];
    } else {
        [self.bottomBtn setTitle:@"分享给微信好友" forState:UIControlStateNormal];
        [self.bottomBtn setImage:[UIImage imageNamed:@"wechat_session"] forState:UIControlStateNormal];
    }
}

- (void)setImages:(NSArray<UIImage *> *)images
{
    _images = images;
    
    [self.collectionView reloadData];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        for (NSInteger index = 0; index < images.count; index++) {
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:0];
            [self.collectionView selectItemAtIndexPath:indexPath animated:false scrollPosition:UICollectionViewScrollPositionNone];
            //            [self collectionView:self.collectionView didSelectItemAtIndexPath:indexPath];
        }
    });
}



//- (void)chooseBtnClicked:(UIButton *)btn
//{
//    btn.selected = !btn.isSelected;
//    
//}

- (void)bottomBtnClicked
{
    NSArray *indexPaths = [self.collectionView indexPathsForSelectedItems];
    if (indexPaths.count == 0) {
        [SVProgressHUD showInfoWithStatus:@"请选择需要分享的图片"];
        return;
    }
    
    if (self.bottomBtnBlock) {
        NSMutableArray *images = [NSMutableArray array];
        for (NSIndexPath *indexPath in indexPaths) {
            [images addObject:self.images[indexPath.item]];
        }
        self.bottomBtnBlock(images);
        
        [self dismiss];
    }
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.images.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    FKShareImageItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    cell.data = self.images[indexPath.item];
    return cell;
}

//#pragma mark - UICollectionViewDelegateFlowLayout
//- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    
//}

- (void)show
{
    self.backgroundColor = fkColorAlpha(0, 0, 0, 0.5);
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    self.frame = [UIScreen mainScreen].bounds;
    [window addSubview:self];
    
}

- (void)dismiss
{
    [self removeFromSuperview];
}

@end
